package com.tonyodev.fetch2core;

import ohos.utils.Parcel;
import ohos.utils.Sequenceable;

import java.io.Serializable;

/**
 * 下载块
 *
 * @since 2021-05-20
 */
public class DownloadBlockInfo implements DownloadBlock, Serializable {
    /**
     * 序列化
     */
    public static final Sequenceable.Producer
        PRODUCER = new Sequenceable.Producer() {
        public DownloadBlockInfo createFromParcel(Parcel in) {
            // Initialize an instance first, then do customized unmarshlling.
            DownloadBlockInfo instance = new DownloadBlockInfo();
            instance.unmarshalling(in);
            return instance;
        }
    };
    private int downloadId = -1;
    private int blockPosition = -1;
    private long startByte = -1L;
    private long endByte = -1L;
    private long downloadedBytes = -1L;

    @Override
    public DownloadBlock copy() {
        DownloadBlockInfo downloadBlockInfo = new DownloadBlockInfo();
        downloadBlockInfo.downloadId = downloadId;
        downloadBlockInfo.blockPosition = blockPosition;
        downloadBlockInfo.startByte = startByte;
        downloadBlockInfo.endByte = endByte;
        downloadBlockInfo.downloadedBytes = downloadedBytes;
        return (DownloadBlock) downloadBlockInfo;
    }

    public void setDownloadId(int downloadId) {
        this.downloadId = downloadId;
    }

    public void setBlockPosition(int blockPosition) {
        this.blockPosition = blockPosition;
    }

    public void setStartByte(long startByte) {
        this.startByte = startByte;
    }

    public void setEndByte(long endByte) {
        this.endByte = endByte;
    }

    public void setDownloadedBytes(long downloadedBytes) {
        this.downloadedBytes = downloadedBytes;
    }

    @Override
    public int getDownloadId() {
        return downloadId;
    }

    @Override
    public int getBlockPosition() {
        return blockPosition;
    }

    @Override
    public long getStartByte() {
        return startByte;
    }

    @Override
    public long getEndByte() {
        return endByte;
    }

    @Override
    public long getDownloadedBytes() {
        return downloadedBytes;
    }

    @Override
    public int getProgress() {
        return FetchCoreUtils.calculateProgress(this.getDownloadedBytes(), this.getEndByte() - this.getStartByte());
    }

    @Override
    public boolean marshalling(Parcel out) {
        return out.writeInt(downloadId) && out.writeInt(blockPosition) && out.writeLong(startByte)
            && out.writeLong(endByte) && out.writeLong(downloadedBytes);
    }

    @Override
    public boolean unmarshalling(Parcel in) {
        this.downloadId = in.readInt();
        this.blockPosition = in.readInt();
        this.downloadedBytes = in.readLong();
        this.endByte = in.readLong();
        this.startByte = in.readByte();
        return true;
    }

    @Override
    public boolean equals(Object other) {
        if ((DownloadBlockInfo) this == other) {
            return true;
        } else if (other == null || other.getClass() != getClass()) {
            return false;
        } else if (other == null) {
            throw new NullPointerException("null cannot be cast to non-null type com.tonyodev.fetch2core.DownloadBlockInfo");
        } else {
            DownloadBlockInfo var = (DownloadBlockInfo) other;
            if (this.getDownloadId() != var.getDownloadId()) {
                return false;
            } else if (this.getBlockPosition() != var.getBlockPosition()) {
                return false;
            } else if (this.getStartByte() != var.getStartByte()) {
                return false;
            } else if (this.getEndByte() != var.getEndByte()) {
                return false;
            } else {
                return this.getDownloadedBytes() == var.getDownloadedBytes();
            }
        }
    }

    @Override
    public int hashCode() {
        int result = this.getDownloadId();
        result = 31 * result + this.getBlockPosition();
        result = 31 * result + Long.valueOf(this.getStartByte()).hashCode();
        result = 31 * result + Long.valueOf(this.getEndByte()).hashCode();
        result = 31 * result + Long.valueOf(this.getDownloadedBytes()).hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "DownloadBlock(downloadId=" + this.getDownloadId() + ", blockPosition=" + this.getBlockPosition() + ", "
            + "startByte=" + this.getStartByte() + ", endByte=" + this.getEndByte() + ", downloadedBytes=" + this.getDownloadedBytes() + ')';
    }
}
